package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"mxshop_srvs/goods_srv/proto"
)

var goodsClient proto.GoodsClient
var conn *grpc.ClientConn

func main() {
	Init()
	//TestGoodsList()
	//TestCreateGoods()
	TestUpdateGoods()
	//conn.Close()
}

func TestGoodsList() {
	list, err := goodsClient.GoodsList(context.Background(), &proto.GoodsFilterRequest{
		//KeyWords: "苹果",
		//TopCategory: 130358,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("total:%d", list.Total))
	for _, goods := range list.Data {
		fmt.Println(fmt.Sprintf("goodsId:%d,goodsName:%s,shopPrice:%f", goods.Id, goods.Name, goods.ShopPrice))
	}
}

func TestCreateGoods() {
	goods, err := goodsClient.CreateGoods(context.Background(), &proto.CreateGoodsInfo{
		Id:              0,
		Name:            "测试商品",
		GoodsSn:         "SNK#4234",
		Stocks:          10,
		MarketPrice:     15,
		ShopPrice:       20,
		GoodsBrief:      "xxadsd",
		GoodsDesc:       "234234",
		ShipFree:        false,
		Images:          nil,
		DescImages:      nil,
		GoodsFrontImage: "",
		IsNew:           false,
		IsHot:           false,
		OnSale:          false,
		CategoryId:      130366,
		BrandId:         616,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(goods)
}

func TestUpdateGoods() {
	goodsClient.UpdateGoods(context.Background(), &proto.CreateGoodsInfo{
		Id:              844,
		Name:            "test goods 11",
		GoodsSn:         "SNK#4256",
		Stocks:          88,
		MarketPrice:     88,
		ShopPrice:       90,
		GoodsBrief:      "good",
		GoodsDesc:       "hello",
		ShipFree:        false,
		Images:          nil,
		DescImages:      nil,
		GoodsFrontImage: "",
		IsNew:           false,
		IsHot:           false,
		OnSale:          false,
		CategoryId:      130366,
		BrandId:         616,
	})
}

// 初始化
func Init() {
	var err error
	conn, err := grpc.Dial("192.168.31.78:50052", grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	goodsClient = proto.NewGoodsClient(conn)
}
